//
//  MCDietBaseVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/23.
//

import UIKit
import SnapKit
import QMUIKit
import JXSegmentedView
import Toast_Swift
import ObjectMapper
import Kingfisher
import MJRefresh


class MCDietBaseVc: MCBaseVc {
    
    var label:String?
    
    var indexName:String?
    
    let mcDietBaseIdentifier = "mcDietBaseIdentifier"

    var dietCollectionView:UICollectionView!
    
    private var dataSource:Array<MCMcdiet> = [MCMcdiet]()
    
    
    var current:Int = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        
        let flowLayoutView = UICollectionViewFlowLayout.init()
        
        dietCollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayoutView)
        dietCollectionView.dataSource = self
        dietCollectionView.delegate = self
        self.view.addSubview(dietCollectionView)
        dietCollectionView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(0.0)
            make.right.equalTo(view.snp.right).offset(0.0)
            make.top.equalTo(view.snp.top).offset(0.0)
            make.bottom.equalTo(view.snp.bottom).offset(0.0)
        }
        
        self.dietCollectionView.register(UINib.init(nibName: "MCDietCell", bundle: Bundle.main), forCellWithReuseIdentifier: mcDietBaseIdentifier)
        
        
        

        self.dietCollectionView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.current = 1
            self.loadWithSubViews(current: self.current)
        })
        
        self.dietCollectionView.mj_header?.beginRefreshing()
        
        self.dietCollectionView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {
            self.loadWithSubViews(current: self.current)
        })
        
        
    }
    
            
    func loadWithSubViews(current:Int?) -> Void {
        NetWorkRequest(MCDietApi.dietPageList(current:current, labels:label)) {ResponseModel in
            self.dietCollectionView.mj_header?.endRefreshing()
            self.dietCollectionView.mj_footer?.endRefreshing()
            if ResponseModel.code == 200 {
                let listModel = MCDietListModel(JSONString: ResponseModel.dataString)
                if current == 1 {
                    self.dataSource.removeAll()
                    self.dataSource.append(contentsOf: listModel?.mcdiets ?? [])
                }else{
                    self.current += 1
                    self.dataSource.append(contentsOf: listModel?.mcdiets ?? [])
                    self.dietCollectionView.reloadData()
                }
                
                self.dietCollectionView.reloadData()
            }else{
                self.view.makeToast("网络失败",position: .center)
            }
        } failureCallback: { ResponseModel in
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    
}

extension MCDietBaseVc:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (UIScreen.main.bounds.size.width - 26 ) / 2, height: (UIScreen.main.bounds.size.width - 26 ) / 2 + 60)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:MCDietCell = collectionView.dequeueReusableCell(withReuseIdentifier: mcDietBaseIdentifier, for: indexPath) as! MCDietCell
        
        let dietModel:MCMcdiet = self.dataSource[indexPath.row]
        
        cell.titleLabel.text = dietModel.zhName
        cell.subTitleLabel.text = "≈ \(String(describing: dietModel.calorie!)) Kcal"
        cell.headeImageView.kf.setImage(with: URL(string: dietModel.previewUrl ?? ""))
        
        return cell
    }
        
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let dietModel:MCMcdiet = self.dataSource[indexPath.row]
        let detailVc = MCFoodDetailVc.init()
        detailVc.ids = dietModel.id
        detailVc.zhName = dietModel.zhName
        detailVc.labels = indexPath.row
        self.navigationController?.pushViewController(detailVc, animated: true)
       
    }
    
    
}

extension MCDietBaseVc:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
